#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/12/5 13:35
# @Author  : Andrewq
# @Site    : nope
# @File    : employeeResource.py
# @Software: PyCharm

from flask import jsonify, request

from ..models.EmployeeInfoModel import EmployeeInfoManager
from ..models.OrderInfoModel import OrderInfoManager
from ..utils.response_code import RET


class EmployeeResource():

    @classmethod
    def get_person_order_statistics(cls):

        result_dict = OrderInfoManager.get_person_order_statistics()

        if result_dict.get("code") != "200":
            return jsonify(code=result_dict.get("code"), msg=result_dict.get("message"), data=[])

        return jsonify(code=RET.OK, msg=result_dict.get("message"), data=result_dict.get("data"))

    @classmethod
    def get_employee_all_data(cls):

        result_dict = EmployeeInfoManager.get_employee_all_data()

        if result_dict.get("code") != "200":
            return jsonify(code=result_dict.get("code"), msg=result_dict.get("message"), data=[])

        return jsonify(code=RET.OK, msg=result_dict.get("message"), data=result_dict.get("data"))

    @classmethod
    def add_employee_info(cls):
        user_name = request.form.get('UserName')
        department_name = request.form.get('DepartmentName')

        if not all([user_name, department_name]):
            return jsonify(code=RET.PARAMERR, msg='缺失参数user_name, department_name', data=[])

        kwargs = {
            'UserName': user_name,
            'DepartmentName': department_name
        }

        result_dict = EmployeeInfoManager.add_employee_info(**kwargs)

        if result_dict.get("code") != "200":
            return jsonify(code=result_dict.get("code"), msg=result_dict.get("message"), data=[])

        return jsonify(code=RET.OK, msg=result_dict.get("message"), data=result_dict.get("data"))

    @classmethod
    def delete_employee_info(cls):
        user_ID = request.form.get('UserID')

        if not all([user_ID]):
            return jsonify(code=RET.PARAMERR, msg='缺失参数user_ID', data=[])

        kwargs = {
            'UserID': user_ID,
        }

        result_dict = EmployeeInfoManager.delete_employee_info(**kwargs)

        if result_dict.get("code") != "200":
            return jsonify(code=result_dict.get("code"), msg=result_dict.get("message"), data=[])

        return jsonify(code=RET.OK, msg=result_dict.get("message"), data=result_dict.get("data"))

    @classmethod
    def delete_employee_info_list(cls):
        data = eval(request.form.get('data'))

        if not all([data]):
            return jsonify(code=RET.PARAMERR, msg='缺失参数data', data=[])

        kwargs = {
            'data': data
        }

        result_dict = EmployeeInfoManager.delete_employee_info_list(**kwargs)

        if result_dict.get("code") != "200":
            return jsonify(code=result_dict.get("code"), msg=result_dict.get("message"), data=[])

        return jsonify(code=RET.OK, msg=result_dict.get("message"), data=result_dict.get("data"))

    @classmethod
    def edit_employee_info(cls):
        user_ID = request.form.get('UserID')
        user_name = request.form.get('UserName')
        department_name = request.form.get('DepartmentName')

        if not all([user_ID, user_name, department_name]):
            return jsonify(code=RET.PARAMERR, msg='缺失参数user_ID', data=[])

        kwargs = {
            'UserID': user_ID,
            'UserName': user_name,
            'DepartmentName': department_name
        }

        result_dict = EmployeeInfoManager.edit_employee_info(**kwargs)

        if result_dict.get("code") != "200":
            return jsonify(code=result_dict.get("code"), msg=result_dict.get("message"), data=[])

        return jsonify(code=RET.OK, msg=result_dict.get("message"), data=result_dict.get("data"))

    @classmethod
    def get_employee_info_by_tags(cls):
        tag_name = request.args.get('tag')
        keyword = request.args.get('keyword')

        if all([tag_name, keyword]):
            args = {
                'tag': tag_name,
                'keyword': keyword
            }
        else:
            args = {}

        result_dict = EmployeeInfoManager.get_employee_info_by_tags(**args)

        if result_dict.get("code") != "200":
            return jsonify(code=result_dict.get("code"), msg=result_dict.get("message"), data=[])

        return jsonify(code=RET.OK, msg=result_dict.get("message"), data=result_dict.get("data"))


